<template>
  <div>
    <div class="bg"></div>
    <van-nav-bar title="消息" class="navbar">
      <template #left>
        <van-icon
          name="arrow-left"
          class="icon"
          size="20"
          @click="onClickLeft"
        />
      </template>
    </van-nav-bar>
    <div class="wrapper">
      <div class="item" v-for="item in list" :key="item.id">
        <div class="time">
          {{ time | FormatTime }}
        </div>
        <div class="content">
          您的借书《{{ item.book.book_name }}》还有{{
            item.shengyuTime
          }}天到期，请注意完成退书操作
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      time: new Date().getTime(),
    };
  },
  async created() {
    this.list = await this.getBookInfo();
    console.log(this.list);
  },
  methods: {
    shengyuTime(createTime, lendTime) {
      lendTime = lendTime * 1000 * 60 * 60 * 24;
      createTime = new Date(createTime).getTime();
      let t = new Date().getTime() - createTime;
      if (lendTime - t > 0) {
        var time = (lendTime - t) / (1000 * 3600 * 24);
        return Math.round(time);
      }
      return -1;
    },
    async getBookInfo() {
      let { token } = this.$store.state;
      let { username } = this.$store.state.userInfo;
      let res = await this.$http({
        url: "/book/borrow",
        headers: {
          Authorization: token,
        },
      });
      res = res.map((item) => {
        return {
          ...item,
          shengyuTime: this.shengyuTime(item.create_time, item.book.lend_time),
        };
      });
      //过滤出借阅天数小于10天的
      res = res.filter((item) => {
        return (
          item.shengyuTime > 0 &&
          item.shengyuTime < 8 &&
          item.borrower == username
        );
      });
      return res;
    },
    onClickLeft() {
      this.$router.back();
    },
  },
};
</script>

<style scoped>
.navbar {
  position: sticky;
  top: 0;
  left: 0;
}
.icon {
  color: #808080;
}
.bg {
  background-color: #e7e6e6;
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: -1;
}
.item {
  background-color: #ffffff;
  width: 90%;
  margin: 0 auto;
  margin-top: 1.25rem;
  border-radius: 0.625rem;
  overflow: hidden;
  position: relative;
}
.time {
  font-size: 0.8125rem;
  background-color: #5a78ec;
  width: 4.6875rem;
  text-align: left;
  padding-left: 0.7rem;
  /* padding-right: 0.46875rem; */
  color: #ffffff;
  height: 1.25rem;
  line-height: 1.25rem;
}
.content {
  padding: 0.9375rem 1.25rem;
  font-size: 0.75rem;
}
</style>